package generator.gui;

import generator.model.GeneratorModel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;

public class GeneratorView extends JFrame {

	public static final Color BACKGROUND_COLOR = new Color (176, 196, 222);
	
	private JTextArea commercial1;
	private JTextArea commercial2;
	private JTextArea news1;
	private JTextArea news2;
	private JTextArea supplement1;
	private JTextArea supplement2;
	
	private JButton generateAll;
	private JButton pdfButton;
	
	private JRadioButton standard;
	private JRadioButton aktuella;
	
	protected Icon locked;
	protected Icon open;
		
	private boolean useStandardNews = false;
	
	private JButton[] lockButtons = new JButton[6];
	private JPanel buttonPanels[] = new JPanel[6];

	private GeneratorModel model;
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public GeneratorView(GeneratorModel model) {
		
		this.model = model;
		
		this.setTitle("Löpsedelsgenerator v.1 - kicking ass! (as usual)");
		this.setLayout(new BorderLayout());

		locked = new ImageIcon("./images/locked.jpg");
		open = new ImageIcon("./images/open.jpg");
		
		Font big = new Font("Georgia", Font.BOLD, 24);
		Font big2 = new Font("Georgia", Font.PLAIN, 24);
		Font medium = new Font("SansSerif", Font.PLAIN, 12);
				
		//Tidningsarea
		JPanel newsPaper = new JPanel();
		newsPaper.setLayout(new GridLayout(8,1));
		newsPaper.setBackground(BACKGROUND_COLOR);
		
		//sets the logo
		JLabel logo = new JLabel(new ImageIcon("./images/logo2.jpg"));
		JPanel logoLabel = new JPanel();
		logoLabel.setLayout (new BorderLayout());
		logoLabel.setBackground (BACKGROUND_COLOR);
		logoLabel.add( logo, BorderLayout.WEST );
		newsPaper.add (logoLabel);
		
		//sets news-areas	
		commercial1 = new JTextArea("Medskick - första delen");
		commercial1.setFont(big);
		commercial1.setBackground (BACKGROUND_COLOR);
		newsPaper.add(commercial1);
		
		commercial2 = new JTextArea("Medskick - andra delen");
		commercial2.setFont(big2);
		commercial2.setBackground (BACKGROUND_COLOR);
		newsPaper.add(commercial2);
		
		news1 = new JTextArea("Huvudnyhet - första delen");
		news1.setFont(big);
		news1.setBackground (BACKGROUND_COLOR);
		newsPaper.add(news1);
		
		news2 = new JTextArea("Huvudnyhet - första delen");
		news2.setFont(big2);
		news2.setBackground (BACKGROUND_COLOR);
		newsPaper.add(news2);
		
		supplement1 = new JTextArea("Bilaga - första delen");
		supplement1.setFont(big);
		supplement1.setBackground (BACKGROUND_COLOR);
		newsPaper.add(supplement1);
		
		supplement2 = new JTextArea("Bilaga - andra delen");
		supplement2.setFont(big2);
		supplement2.setBackground (BACKGROUND_COLOR);
		newsPaper.add(supplement2);
		
		newsPaper.add(new JLabel(""));
		
		add(newsPaper, BorderLayout.CENTER);
		
		this.add(createLockPanel(), BorderLayout.WEST);
		
		
		//Instruktionsdel - statisk text
		JPanel instructions = new JPanel();
		instructions.setLayout(new FlowLayout());
		instructions.setBackground(new Color (255, 255, 255));
		instructions.setPreferredSize (new Dimension(200, 600));
		
		JTextArea helpText = new JTextArea(
		"SÅ HÄR GÖR DU: \nTryck på knappen för att generera nyheter. " +
		"Om du är nöjd med en viss nyheter, klicka på låset till vänster om nyheten. " +
		"De olåsta nyheterna genereras därefter om nästa gång du trycker på knappen. \n\n" +
		"När du är nöjd, tryck på pdf-knappen så har du en färdig pdf."
		);
		helpText.setLineWrap(true);
		helpText.setWrapStyleWord(true);
		helpText.setFont(medium);
		helpText.setPreferredSize(new Dimension(180, 300));
		instructions.add(helpText);
		
		ButtonGroup radioGroup = new ButtonGroup();
		aktuella = new JRadioButton("Aktuella nyheter");
		aktuella.addActionListener(new GenerateActionListener());
		aktuella.setBackground(Color.white);
		radioGroup.add(aktuella);
		instructions.add(aktuella);
	
		standard = new JRadioButton("Standardnyheter");
		standard.addActionListener(new GenerateActionListener());
		standard.setBackground(Color.white);
		radioGroup.add(standard);
		instructions.add(standard);
		
		aktuella.setSelected(true);
		
		
		//generate-button
		generateAll = new JButton("Generera nyheter");
		generateAll.addActionListener(new GenerateActionListener());
		instructions.add(generateAll);
		generateAll.setPreferredSize(new Dimension(150, 20));
		
		
		//PDF-button
		pdfButton = new JButton("Skapa PDF!");		
		pdfButton.setEnabled(false);
		pdfButton.addActionListener(new GenerateActionListener());
		pdfButton.setPreferredSize(new Dimension(150, 20));
		instructions.add(pdfButton);

		JTextArea authorText = new JTextArea(
				"\n\n\n\nav: Jenny \"Jippie\" Cahier " +
				"\n  & Peter \"Peter\" Rosberg"
				);
		authorText.setLineWrap(true);
		authorText.setWrapStyleWord(true);
		authorText.setFont(medium);
		authorText.setPreferredSize(new Dimension(180, 300));
		authorText.setAlignmentX(RIGHT_ALIGNMENT);
		authorText.setAlignmentY(BOTTOM_ALIGNMENT);
		instructions.add(authorText);
		
		this.add(instructions, BorderLayout.EAST);
		
		this.setSize(1200, 600);
		setVisible(true);
		
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);

	}
	
	private JPanel createLockPanel() {
		JPanel lockPanel = new JPanel();
		lockPanel.setLayout(new GridLayout(8,1));
		//lockPanel.setPreferredSize(new Dimension (200, 600));
		lockPanel.setBackground(BACKGROUND_COLOR);
		
		
		for(int i=0; i<lockButtons.length; i++) {
			lockButtons[i] = new JButton();
			lockButtons[i].setIcon(open);
			buttonPanels[i] = new JPanel();
			buttonPanels[i].setBackground(BACKGROUND_COLOR);
			buttonPanels[i].setPreferredSize(new Dimension(40, 40));
			lockButtons[i].setPreferredSize(new Dimension(open.getIconHeight(),open.getIconWidth()));
			buttonPanels[i].add(lockButtons[i]);
			lockButtons[i].addActionListener(new GenerateActionListener());
		}
		
		lockPanel.add(new JLabel(""));
		lockPanel.add(buttonPanels[0]);
		lockPanel.add(buttonPanels[1]);
		lockPanel.add(buttonPanels[2]);
		lockPanel.add(buttonPanels[3]);
		lockPanel.add(buttonPanels[4]);
		lockPanel.add(buttonPanels[5]);
		lockPanel.add(new JLabel(""));
		
		return lockPanel;
		
	}
	
	
	public class GenerateActionListener implements ActionListener {

		
		public void actionPerformed(ActionEvent aE) {
			if(aE.getSource() == generateAll) {
				
				model.updateAll(useStandardNews);
				pdfButton.setEnabled(true);
				updateView();
				
			} else if((aE.getSource() == aktuella)||(aE.getSource() == standard)) {
					
					if(aktuella.isSelected()) {
						useStandardNews = false;
					} else {
						useStandardNews = true;
					}
					
			} else if(aE.getSource() == pdfButton) {

				// PDF-anrop här:
				System.out.println("PDF!");
				try {
					model.createPDF();
					System.out.println("- made PDF!");
					Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler posters\\GeneratedPoster.pdf");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			
			//Check lockbuttons
			for(int i=0; i<lockButtons.length; i++) {
				if(aE.getSource() == lockButtons[i]) {
					
					//Toggle button
					model.setLock(i, !model.getLock(i));
					//Change text
					if(model.getLock(i)) {
						 
						lockButtons[i].setIcon(locked);
					} else {
						lockButtons[i].setIcon(open);
					}
				}
			}

		}
		
		private void updateView() {
			
			for(int i=0; i<lockButtons.length; i++) {
				if(model.getLock(i)) {
					lockButtons[i].setIcon(locked);
				} else {
					lockButtons[i].setIcon(open);
				}
			}
			
			
			String[] newsItems = model.getNewsItems();
			
			news1.setText(newsItems[model.MAIN_NEWS]);
			news2.setText("- " + newsItems[model.MAIN_NEWS_SLOGAN]);
		
			supplement1.setText(newsItems[model.SUPPLEMENT_TITLE]);
			supplement2.setText(newsItems[model.SUPPLEMENT_SLOGAN]);
			
			commercial1.setText(newsItems[model.SIDE_OFFER_ITEM]);
			commercial2.setText ("- " + newsItems[model.SIDE_OFFER_SLOGAN]);
		}

	}
	
}